package com.ruanyun.modular.downWord;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.modular.admin.entity.*;
import com.ruanyun.modular.admin.mapper.TBasicinfoMapper;
import com.ruanyun.modular.admin.model.result.*;
import com.ruanyun.modular.admin.service.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/downWord")
public class DownWord extends BaseController {
    @Autowired
    private TCommitReportService commitReportService;

    /*生成报告*/
    @RequestMapping(value = "downReport", produces = "application/octet-stream")
    @ResponseBody
    public void downReport(Integer companyId, HttpServletResponse resp) throws IOException {

        TCommitReport commitReport = commitReportService.getDetailByCid(companyId);
        if (commitReport != null) {
            @Deprecated
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf8");
            configuration.setClassForTemplateLoading(this.getClass(), "/Template");
            Template template = configuration.getTemplate("template.ftl");

            /*获取导出数据*/
            Map<String, Object> dataMap = commitReportService.dataMap(companyId);

            //开始导出
            String fileName = "一企一策";

            try {
                ToWord.toWord(dataMap, fileName, template, resp); //生成word工具类
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }

    /*生成报告*/
    @RequestMapping(value = "exportData", produces = "application/octet-stream")
    @ResponseBody
    public ResponseData exportData(Integer companyId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResponseData responseData = commitReportService.exportData(companyId, request, response);
        return responseData;
    }

}
